package com.hup.utils.swing.customView.viewTable;

import com.hup.utils.commons.start.LookThemeUtil;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;

/**
 * 实现 {@link ViewTableRowBo}在[获取表头,列宽,列clz]时的功能;
 * 解决在增减列时手工处理index的问题
 */
public class TColumnInfo {
    /**
     * 供外部更新列名
     */
    @Setter
    private String name;
    @Getter
    private Class<?> clz;
    @Getter
    private Integer width;
    @Getter
    private int columnIndex;

    /**
     * 根据info获取表头
     */
    public static String[] getHeaderStr(ArrayList<TColumnInfo> lstInfo) {
        String[] res = new String[lstInfo.size()];
        for (int i = lstInfo.size() - 1; i >= 0; i--) {
            res[i] = lstInfo.get(i).name;
        }
        return res;
    }

    /**
     * 根据info获取列宽
     */
    public static Integer[] getWidth(ArrayList<TColumnInfo> lstInfo) {
        Integer[] res = new Integer[lstInfo.size()];
        for (int i = lstInfo.size() - 1; i >= 0; i--) {
            res[i] = lstInfo.get(i).width;
        }
        return res;
    }

    public static class Builder {
        private static final float scale = LookThemeUtil.getScreenScale();
        private final Integer defWidth;
        private final ArrayList<TColumnInfo> lstInfo;

        public Builder(Integer defWidth) {
            this.defWidth = defWidth;
            lstInfo = new ArrayList<>();
        }

        public Builder add(String name, Class<?> clz) {
            add0(name, clz, defWidth);
            return this;
        }

        public TColumnInfo add0(String name, Class<?> clz) {
            return add0(name, clz, defWidth);
        }

        /**
         * @param clz notNull
         */
        public Builder add(String name, Class<?> clz, Integer width) {
            add0(name, clz, width);
            return this;
        }

        /**
         * @param clz notNull
         */
        public TColumnInfo add0(String name, Class<?> clz, Integer width) {
            TColumnInfo info = new TColumnInfo();
            info.name = name;
            info.clz = clz;
            if (width != null) {
                info.width = (int) (width * scale);
            }
            lstInfo.add(info);
            info.columnIndex = lstInfo.size() - 1;
            return info;
        }

        public ArrayList<TColumnInfo> get() {
            return lstInfo;
        }

        public static Builder builder(Integer defWidth) {
            return new Builder(defWidth);
        }
    }
}